import pygame
import random
from tools import *

wall_size = 24
width, height = wall_size * 26, wall_size * 26
size = (width + wall_size * 4, height)
screen = pygame.display.set_mode(size)
fps = 60
pygame.mixer.init()
# 子弹发射音效
bullet_sound = pygame.mixer.Sound("sound/fire.wav")
bullet_sound.set_volume(0.5)


class Home(pygame.sprite.Sprite):
    """老家类"""

    def __init__(self):
        super().__init__()
        self.active = True
        self.crossover = False
        self.frame = 0
        self.image1 = pygame.image.load('images/home/home1.png')
        self.image2 = pygame.image.load('images/home/home_destroyed.png')
        self.rect = self.image1.get_rect()
        self.rect.centerx = width / 2
        self.rect.bottom = height
        self.all_image = pygame.image.load('images/others/boom_dynamic.png')
        self.boom_image = self.all_image.subsurface((0, 0), (40, 40))
        self.boom_rect = self.boom_image.get_rect()
        self.boom_rect.center = self.rect.center
        self.boom_fps = 15


class Tank(pygame.sprite.Sprite):
    """所有坦克类的父类"""

    def __init__(self):
        super().__init__()
        self.blood = 1
        self.speed = 60 / fps
        self.active = True
        self.crossover = False
        self.moving = True
        self.food = False
        self.max_bullet = 1
        self.frame = 1
        self.direction = 'up'
        self.all_image = pygame.image.load('images/playerTank/tank_T1_0.png')
        self.image = self.all_image.subsurface((0, 0), (48, 48))
        self.rect = self.image.get_rect()
        self.all_image2 = pygame.image.load('images/others/boom_dynamic.png')
        self.boom_image = self.all_image2.subsurface((0, 0), (40, 40))
        self.boom_rect = self.boom_image.get_rect()
        self.boom_fps = 15
        self.bullets = pygame.sprite.Group()

    def fire(self):
        """发射子弹"""
        if len(self.bullets) < self.max_bullet:
            bullet = Bullet()
            bullet.direction = self.direction
            if self.direction == 'up':
                bullet.rect.y = self.rect.y
                bullet.rect.centerx = self.rect.centerx
            elif self.direction == 'down':
                bullet.rect.bottom = self.rect.bottom
                bullet.rect.centerx = self.rect.centerx
            elif self.direction == 'left':
                bullet.rect.x = self.rect.x
                bullet.rect.centery = self.rect.centery
            elif self.direction == 'right':
                bullet.rect.right = self.rect.right
                bullet.rect.centery = self.rect.centery
            self.bullets.add(bullet)


class PlayerTank(Tank):
    """玩家坦克"""

    def __init__(self, player_num=1):
        super().__init__()
        self.player_num = player_num
        self.life = 3
        self.moving = True
        self.invincible = False
        self.protected = False
        self.protected_start = 0
        self.protected_end = 0
        self.level = 0
        self.speed = 120 / fps
        if self.player_num == 1:
            self.image_name = 'images/playerTank/tank_T1_0.png'
        elif self.player_num == 2:
            self.image_name = 'images/playerTank/tank_T2_0.png'
        # self.image_list = []  # 存放子图片的列表
        # for i in range(8):
        #     row = i // 2
        #     col = i % 2
        #     self.region = (48*col, 48*row, 48, 48)  # 图片坐标和宽度高度
        #     self.sub_image = pygame.Surface.subsurface(self.all_image, self.region)
        #     self.image_list.append(self.sub_image)j
        self.all_image = pygame.image.load(self.image_name)
        self.image = self.all_image.subsurface((0, 0), (48, 48))
        self.rect = self.image.get_rect()
        self.rect.centerx = wall_size * 9
        self.rect.bottom = height
        # 玩家坦克移动时的发动机声
        self.driving_sound = pygame.mixer.Sound('sound/EngineDriving.wav')
        self.driving_sound.set_volume(0.1)
        self.protective_cover = Protect()
        self.protective_cover.rect = self.rect

    def upgrade(self):
        if self.player_num == 1:
            if self.level == 0:
                self.image_name = 'images/playerTank/tank_T1_0.png'
            elif self.level == 1:
                self.image_name = 'images/playerTank/tank_T1_1.png'
            elif self.level == 2:
                self.image_name = 'images/playerTank/tank_T1_2.png'
            elif self.level == 3:
                self.image_name = 'images/playerTank/tank_T1_3.png'
                self.blood = 2
        elif self.player_num == 2:
            if self.level == 0:
                self.image_name = 'images/playerTank/tank_T2_0.png'
            elif self.level == 1:
                self.image_name = 'images/playerTank/tank_T2_1.png'
            elif self.level == 2:
                self.image_name = 'images/playerTank/tank_T2_2.png'
            elif self.level == 3:
                self.image_name = 'images/playerTank/tank_T2_3.png'
                self.blood = 2
        self.all_image = pygame.image.load(self.image_name)

    def move(self):
        keys = pygame.key.get_pressed()
        if self.player_num == 1:
            # 向上移动
            if keys[pygame.K_w]:
                self.direction = 'up'
                if self.rect.y > 0:
                    self.rect.y -= self.speed
                else:
                    self.rect.y = 0
                # 坦克行进的音效
                if self.frame == 1:
                    self.driving_sound.play()
                elif self.frame == fps:
                    self.driving_sound.stop()

                # 坦克移动的动画效果, 15 就是每秒动画切换的次数
                if self.frame * 15 // fps % 2:
                    self.image = self.all_image.subsurface((0, 0), (48, 48))
                else:
                    self.image = self.all_image.subsurface((48, 0), (48, 48))
                if self.frame < fps:
                    self.frame += 1
                else:
                    self.frame = 1
            # 向下移动
            elif keys[pygame.K_s]:
                self.direction = 'down'
                if self.rect.bottom < height:
                    self.rect.y += self.speed
                else:
                    self.rect.bottom = height
                # 坦克移动的动画效果
                if self.frame * 15 // fps % 2:
                    self.image = self.all_image.subsurface((0, 48), (48, 48))
                else:
                    self.image = self.all_image.subsurface((48, 48), (48, 48))
                if self.frame == 1:
                    self.driving_sound.play()
                elif self.frame == fps:
                    self.driving_sound.stop()
                if self.frame < fps:
                    self.frame += 1
                else:
                    self.frame = 1
            # 向左移动
            elif keys[pygame.K_a]:
                self.direction = 'left'
                if self.rect.x > 0:
                    self.rect.x -= self.speed
                else:
                    self.rect.x = 0
                # 坦克移动的动画效果
                if self.frame * 15 // fps % 2:
                    self.image = self.all_image.subsurface((0, 96), (48, 48))
                else:
                    self.image = self.all_image.subsurface((48, 96), (48, 48))
                if self.frame == 1:
                    self.driving_sound.play()
                elif self.frame == fps:
                    self.driving_sound.stop()
                if self.frame < fps:
                    self.frame += 1
                else:
                    self.frame = 1
            # 向右移动
            elif keys[pygame.K_d]:
                self.direction = 'right'
                if self.rect.right < width:
                    self.rect.x += self.speed
                else:
                    self.rect.right = width
                # 坦克移动的动画效果
                if self.frame * 15 // fps % 2:
                    self.image = self.all_image.subsurface((0, 144), (48, 48))
                else:
                    self.image = self.all_image.subsurface((48, 144), (48, 48))
                if self.frame == 1:
                    self.driving_sound.play()
                elif self.frame == fps:
                    self.driving_sound.stop()
                if self.frame < fps:
                    self.frame += 1
                else:
                    self.frame = 1
        elif self.player_num == 2:
            # 向上移动
            if keys[pygame.K_UP]:
                self.direction = 'up'
                if self.rect.y > 0:
                    self.rect.y -= self.speed
                else:
                    self.rect.y = 0
                # 坦克行进的音效
                if self.frame == 1:
                    self.driving_sound.play()
                elif self.frame == fps:
                    self.driving_sound.stop()
                # 坦克移动的动画效果, 10 就是每秒动画切换的次数
                if self.frame * 15 // fps % 2:
                    self.image = self.all_image.subsurface((0, 0), (48, 48))
                else:
                    self.image = self.all_image.subsurface((48, 0), (48, 48))
                if self.frame < fps:
                    self.frame += 1
                else:
                    self.frame = 1
            # 向下移动
            elif keys[pygame.K_DOWN]:
                self.direction = 'down'
                if self.rect.bottom < height:
                    self.rect.y += self.speed
                else:
                    self.rect.bottom = height
                # 坦克移动的动画效果
                if self.frame * 15 // fps % 2:
                    self.image = self.all_image.subsurface((0, 48), (48, 48))
                else:
                    self.image = self.all_image.subsurface((48, 48), (48, 48))
                if self.frame == 1:
                    self.driving_sound.play()
                elif self.frame == fps:
                    self.driving_sound.stop()
                if self.frame < fps:
                    self.frame += 1
                else:
                    self.frame = 1
            # 向左移动
            elif keys[pygame.K_LEFT]:
                self.direction = 'left'
                if self.rect.x > 0:
                    self.rect.x -= self.speed
                else:
                    self.rect.x = 0
                # 坦克移动的动画效果
                if self.frame * 15 // fps % 2:
                    self.image = self.all_image.subsurface((0, 96), (48, 48))
                else:
                    self.image = self.all_image.subsurface((48, 96), (48, 48))
                if self.frame == 1:
                    self.driving_sound.play()
                elif self.frame == fps:
                    self.driving_sound.stop()
                if self.frame < fps:
                    self.frame += 1
                else:
                    self.frame = 1
            # 向右移动
            elif keys[pygame.K_RIGHT]:
                self.direction = 'right'
                if self.rect.right < width:
                    self.rect.x += self.speed
                else:
                    self.rect.right = width
                # 坦克移动的动画效果
                if self.frame * 15 // fps % 2:
                    self.image = self.all_image.subsurface((0, 144), (48, 48))
                else:
                    self.image = self.all_image.subsurface((48, 144), (48, 48))
                if self.frame == 1:
                    self.driving_sound.play()
                elif self.frame == fps:
                    self.driving_sound.stop()
                if self.frame < fps:
                    self.frame += 1
                else:
                    self.frame = 1

    def fire(self):
        """玩家发射子弹"""
        if self.level >= 2:
            self.max_bullet = 2
        else:
            self.max_bullet = 1
        if len(self.bullets) < self.max_bullet:
            bullet = Bullet()
            bullet.direction = self.direction
            bullet.level = self.level
            if self.direction == 'up':
                bullet.rect.y = self.rect.y
                bullet.rect.centerx = self.rect.centerx
            elif self.direction == 'down':
                bullet.rect.bottom = self.rect.bottom
                bullet.rect.centerx = self.rect.centerx
            elif self.direction == 'left':
                bullet.rect.x = self.rect.x
                bullet.rect.centery = self.rect.centery
            elif self.direction == 'right':
                bullet.rect.right = self.rect.right
                bullet.rect.centery = self.rect.centery
            bullet.speed = 300 / fps + 2 * self.level
            self.bullets.add(bullet)
            bullet_sound.play()

    def reset(self):
        if self.life > 0:
            if self.player_num == 1:
                self.rect.centerx = wall_size * 9
                self.rect.bottom = height
            elif self.player_num == 2:
                self.rect.centerx = wall_size * 17
                self.rect.bottom = height
            self.direction = 'up'
            self.image = self.all_image.subsurface((0, 0), (48, 48))
            self.blood = 1
            self.active = True


class EnemyTank(Tank):
    def __init__(self, enemy_num=1):
        super().__init__()
        self.direction = 'down'
        self.enemy_num = enemy_num
        if self.enemy_num == 1:
            self.image_name = 'images/enemyTank/enemy_1_0.png'
            self.speed = 120 / fps
        elif self.enemy_num == 13:
            self.image_name = 'images/enemyTank/enemy_1_3.png'
            self.speed = 120 / fps
            self.food = True
        elif self.enemy_num == 2:
            self.image_name = 'images/enemyTank/enemy_2_0.png'
        elif self.enemy_num == 23:
            self.image_name = 'images/enemyTank/enemy_2_3.png'
            self.food = True
        elif self.enemy_num == 3:
            self.image_name = 'images/enemyTank/enemy_3_0.png'
            self.blood = 3
        elif self.enemy_num == 33:
            self.image_name = 'images/enemyTank/enemy_3_3.png'
            self.blood = 3
            self.food = True
        self.all_image = pygame.image.load(self.image_name)
        self.image = self.all_image.subsurface((0, 48), (48, 48))
        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 0

    def move(self):
        if self.rect.y < 0:
            self.rect.y = 0
        if self.rect.bottom > height:
            self.rect.bottom = height
        if self.rect.x < 0:
            self.rect.x = 0
        if self.rect.right > width:
            self.rect.right = width
        if self.direction == 'up':
            if self.rect.x % wall_size:
                self.rect.x = round(self.rect.x / wall_size) * wall_size
            self.rect.y -= self.speed
            if self.rect.y < 0:
                self.rect.y = 0
                self.direction = random.choice(['down', 'left', 'right'])
            # 移动动画
            if self.frame * 15 // fps % 2:
                self.image = self.all_image.subsurface((0, 0), (48, 48))
            else:
                self.image = self.all_image.subsurface((48, 0), (48, 48))
            if self.frame < fps:
                self.frame += 1
            else:
                self.frame = 1
        elif self.direction == 'down':
            if self.rect.x % wall_size:
                self.rect.x = round(self.rect.x / wall_size) * wall_size
            self.rect.y += self.speed
            if self.rect.bottom > height:
                self.rect.bottom = height
                self.direction = random.choice(['up', 'left', 'right'])
            # 移动动画
            if self.frame * 15 // fps % 2:
                self.image = self.all_image.subsurface((0, 48), (48, 48))
            else:
                self.image = self.all_image.subsurface((48, 48), (48, 48))
            if self.frame < fps:
                self.frame += 1
            else:
                self.frame = 1
        elif self.direction == 'left':
            if self.rect.y % wall_size:
                self.rect.y = round(self.rect.y / wall_size) * wall_size
            self.rect.x -= self.speed
            if self.rect.x < 0:
                self.rect.x = 0
                self.direction = random.choice(['up', 'down', 'right'])
            # 移动动画
            if self.frame * 15 // fps % 2:
                self.image = self.all_image.subsurface((0, 96), (48, 48))
            else:
                self.image = self.all_image.subsurface((48, 96), (48, 48))
            if self.frame < fps:
                self.frame += 1
            else:
                self.frame = 1
        elif self.direction == 'right':
            if self.rect.y % wall_size:
                self.rect.y = round(self.rect.y / wall_size) * wall_size
            self.rect.x += self.speed
            if self.rect.right > width:
                self.rect.right = width
                self.direction = random.choice(['up', 'down', 'left'])
            # 移动动画
            if self.frame * 15 // fps % 2:
                self.image = self.all_image.subsurface((0, 144), (48, 48))
            else:
                self.image = self.all_image.subsurface((48, 144), (48, 48))
            if self.frame < fps:
                self.frame += 1
            else:
                self.frame = 1

    def reselect_direction(self):
        """此路不通，重新选择方向"""
        if self.direction == 'up':
            self.direction = random.choice(['down', 'left', 'right'])
        elif self.direction == 'down':
            self.direction = random.choice(['up', 'left', 'right'])
        elif self.direction == 'left':
            self.direction = random.choice(['up', 'down', 'right'])
        elif self.direction == 'right':
            self.direction = random.choice(['up', 'down', 'left'])


class Bullet(pygame.sprite.Sprite):
    """坦克子弹精灵"""

    def __init__(self):
        super().__init__()
        self.all_image = pygame.image.load('images/others/boom_dynamic.png')
        self.boom_image = self.all_image.subsurface((0, 0), (40, 40))
        self.boom_rect = self.boom_image.get_rect()
        self.boom_fps = 6
        self.rect = pygame.Rect(0, 0, 8, 8)
        self.speed = 300 / fps
        self.direction = 'up'
        self.level = 0
        self.active = True
        self.frame = 0

    def move(self):
        """根据子弹的方向移动"""
        if self.direction == 'up':
            self.rect.y -= self.speed
            # 飞出屏幕范围后销毁
            if self.rect.y < 0:
                self.active = False
        elif self.direction == 'down':
            self.rect.y += self.speed
            if self.rect.bottom > height:
                self.active = False
        elif self.direction == 'left':
            self.rect.x -= self.speed
            if self.rect.left < 0:
                self.active = False
        elif self.direction == 'right':
            self.rect.x += self.speed
            if self.rect.right > width:
                self.active = False


ui_image = pygame.image.load('images/others/UIView1.png')
enemy_num_image = ui_image.subsurface((0, 0), (14, 14))
enemy_num_rect = enemy_num_image.get_rect()
IP_image = ui_image.subsurface((28, 0), (28, 14))
IP_rect = IP_image.get_rect()
IP_rect.x = wall_size * 27 + 1
IP_rect.y = wall_size * 15
IIP_image = ui_image.subsurface((56, 0), (28, 14))
IIP_rect = IIP_image.get_rect()
IIP_rect.x = wall_size * 27 + 1
IIP_rect.y = wall_size * 18
player_life_image = ui_image.subsurface((14, 0), (14, 14))
player1_life_rect = player_life_image.get_rect()
player1_life_rect.x = wall_size * 27
player1_life_rect.y = wall_size * 16
player2_life_rect = player_life_image.get_rect()
player2_life_rect.x = wall_size * 27
player2_life_rect.y = wall_size * 19
flag_image = pygame.image.load('images/others/flag.png')
flag_rect = flag_image.get_rect()
flag_rect.x = wall_size * 27
flag_rect.y = wall_size * 21


class Food(pygame.sprite.Sprite):
    """食物道具"""

    def __init__(self, food_num):
        super().__init__()
        self.food_num = food_num
        if self.food_num == 1:
            self.image = pygame.image.load('images/food/food_boom.png')
        elif self.food_num == 2:
            self.image = pygame.image.load('images/food/food_clock.png')
        elif self.food_num == 3:
            self.image = pygame.image.load('images/food/food_gun.png')
        elif self.food_num == 4:
            self.image = pygame.image.load('images/food/food_iron.png')
        elif self.food_num == 5:
            self.image = pygame.image.load('images/food/food_protect.png')
        elif self.food_num == 6:
            self.image = pygame.image.load('images/food/food_star.png')
        elif self.food_num == 7:
            self.image = pygame.image.load('images/food/food_tank.png')
        self.rect = self.image.get_rect()
        centerx = random.randint(1, 25)
        centery = random.randint(1, 25)
        self.rect.center = (centerx * wall_size, centery * wall_size)


class DestroyEffects(pygame.sprite.Sprite):
    def __init__(self, object):
        super().__init__()
        self.all_image = pygame.image.load('images/others/boom_dynamic.png')
        self.image = self.all_image.subsurface((0, 0), (40, 40))
        self.rect = self.image.get_rect()
        self.rect.centerx = object.rect.centerx
        self.rect.centery = object.rect.centery
        self.boom_fps = object.boom_fps
        self.frame = 0

    def show(self):
        self.image = self.all_image.subsurface((40 * self.frame, 0), (40, 40))
        screen.blit(self.image, self.rect)
        self.frame += 1
        if self.frame >= self.boom_fps:
            self.kill()


class Protect(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.all_image = pygame.image.load('images/others/protect.png')
        self.image = self.all_image.subsurface((0, 0), (48, 48))
        self.rect = self.image.get_rect()
        self.frame = 1
        self.time = 0

    def draw(self, rect):
        screen.blit(self.image, rect)
        if self.frame < fps:
            self.frame += 1
        else:
            self.frame = 1
            self.time += 1
        if self.frame * 10 // fps % 2:
            self.image = self.all_image.subsurface((0, 0), (48, 48))
        else:
            self.image = self.all_image.subsurface((48, 0), (48, 48))
